package com.example.pages;

import java.util.List;

import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.FindBys;

public class HomePage extends InternalPage {
        @FindBy(css = "img[alt=\"Add movie\"]")
        public WebElement iconAddMovie;
        
        @FindBy(css = "img[alt=\"Update all\"]")
        public WebElement iconUpdateAll;
        
        @FindBy(css = "img[alt=\"Export\"]")
        public WebElement iconExport;
        
        @FindBy(css = "img[alt=\"Not own\"]")
        public WebElement iconNotOwn;
        
        @FindBy(id = "search")
        public WebElement search;
        
        @FindBy(id = "n-button")
        public WebElement itemsPerPage;
        
        @FindBy(id = "n-menu")
        public WebElement listOfResultsPerPage;
        
        @FindBys({@FindBy(id = "n-menu"),@FindBy(xpath = "./li[1]/a")})
        public WebElement allResults;
        
        @FindBy(id = "results")
        public WebElement results;
        
        @FindBy(xpath = "//div[@id='results']//div[@class='title']")
        public List<WebElement> linksToFilms;
        
        public AddMoviePage addMovie(){
        	iconAddMovie.click();
    		return MyPageFactory.getPage(driver, AddMoviePage.class);
    	}            

        public ProfilePage goToProfilePage(){
        	menuProfileLink.click();
        	return MyPageFactory.getPage(driver, ProfilePage.class);
        }
        
        public void openFilm(String filmTitle){
    		itemsPerPage.click();
    		allResults.click();
    		selectFilmForEdit(filmTitle);
    	}
        
    	public FilmPage selectFilmForEdit(String filmTitle) {
    		for(WebElement title : linksToFilms){
    			if(title.getText().equals(filmTitle)){
    				title.click();
    				break;
    			}
    		}
    		return MyPageFactory.getPage(driver, FilmPage.class);
    	}
        
        public boolean isOnThisPage(){
    		return search.isDisplayed();
    	}
}
